from typing import List

import pytest

from spider_h5.seleinum_bankend.client.web_client import WebClient
from spider_h5.seleinum_bankend.pages.meta_level.login_page import LoginPage
from spider_h5.seleinum_bankend.pages.sec_level.applet_page import AppletPage
from spider_h5.seleinum_bankend.pages.third_level.applet_manage_page import AppletManagePage
from spider_h5.seleinum_bankend.utils.mongo_utils import DBRoc
from functional import seq

"""
Description: 从云餐后台获取数据到Mongodb
Note:
Warning:
"""


class TestSpiderScrawl(object):
    """
    爬虫获取数据
    """

    """根据小程序ID获取数据"""

    def save_goods_list_accord_to_applet_id(self, client, applet_ids):
        """
        根据小程序ID获取商品列表数据
        Params:
        -client:客户端
        -applet_ids:小程序IDS
        """
        applet_page = LoginPage(client).login(username='admin', password='palmnet2025').turn_applet_page()
        for applet_id in applet_ids:
            applet_page.enter_applet_id(applet_id)
            AppletManagePage(client).gain_goods_list(applet_id=applet_id)

    """根据小程序ID获取商品详情数据"""

    def save_goods_detail_accord_to_applet_id(self, client, applet_ids):
        """
        根据小程序ID获取商品详情数据
        Params:
        -client:客户端
        -applet_ids:小程序IDS
        """
        for applet_id in applet_ids:
            applet_page = LoginPage(client).login(username='admin', password='palmnet2025').turn_applet_page()
            applet_page.enter_applet_id(applet_id)
            AppletManagePage(client).gain_goods_detail(applet_id)  # 注意需要先进入某个店铺才可以进入商品的详情页

    """
    Instance Case
    """

    @pytest.fixture(scope="session")
    def get_client(self):
        """创建数据库连接，整个测试会话共享"""
        print("------开始创建测试客户端--------")
        client = WebClient()
        yield client  # 返回数据库连接对象
        print("------测试完成销毁客户端--------")

    @pytest.mark.dishreturn
    @pytest.mark.parametrize(('applets_ids'), [(
            ['10060', '10066', '10021']
    )])
    def test_save_goods_list_accord_to_applet_id(self, get_client, applets_ids: List[str]):
        """
        保存商品数据根据小程序ID
        """
        self.save_goods_list_accord_to_applet_id(get_client, applets_ids)

    @pytest.mark.dishreturn
    @pytest.mark.parametrize(('applets_ids'), [(
            ['10060', '10066', '10021']
    )])
    def test_save_goods_detail_accord_to_goods_url(self, get_client, applets_ids):
        """
        根据商品的链接获取商品详情
        """
        self.save_goods_detail_accord_to_applet_id(get_client, applets_ids)
